package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.feign.search.SearchFeignClient;
import com.atguigu.gmall.product.mapper.SkuInfoMapper;
import com.atguigu.gmall.product.pojo.*;
import com.atguigu.gmall.product.search.Goods;
import com.atguigu.gmall.product.search.SearchAttr;
import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.product.vo.CategoryTreeVo;
import com.atguigu.gmall.product.vo.SkuInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author h8467
* @description 针对表【sku_info(库存单元表)】的数据库操作Service实现
* @createDate 2022-11-29 16:02:29
*/
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
    implements SkuInfoService{
    @Autowired
    SearchFeignClient searchFeignClient;
    @Autowired
    SkuAttrValueService skuAttrValueService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    BaseTrademarkService baseTrademarkService;

    @Autowired
    BaseCategory1Service baseCategory1Service;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    @Transactional
    public void saveSkuInfo(SkuInfoVo skuInfoVo) {
        Long spuId = skuInfoVo.getSpuId();
        SkuInfo skuInfo = new SkuInfo();
        List<SkuInfoVo.SkuAttrValueListDTO> skuAttrValueListDTOList = skuInfoVo.getSkuAttrValueList();
        List<SkuInfoVo.SkuSaleAttrValueListDTO> skuSaleAttrValueListDTOList = skuInfoVo.getSkuSaleAttrValueList();
        List<SkuInfoVo.SkuImageListDTO> skuImageListDTO = skuInfoVo.getSkuImageList();

        // 1. 上传SkuInfo
        BeanUtils.copyProperties(skuInfoVo,skuInfo);
        baseMapper.insert(skuInfo);
        Long skuId = skuInfo.getId();


        // 2. 上传skuAttrValueList
        List<SkuAttrValue> skuAttrValueList = skuAttrValueListDTOList.stream().map(skuAttrValueListDTO -> {
            SkuAttrValue skuAttrValue = new SkuAttrValue();
            BeanUtils.copyProperties(skuAttrValueListDTO, skuAttrValue);
            skuAttrValue.setSkuId(skuId);
            return skuAttrValue;
        }).collect(Collectors.toList());
        skuAttrValueService.saveBatch(skuAttrValueList);

        // 3. 上传skuSaleAttrValueList
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuSaleAttrValueListDTOList.stream().map(skuSaleAttrValueListDTO -> {
            SkuSaleAttrValue skuSaleAttrValue = new SkuSaleAttrValue();
            skuSaleAttrValue.setSkuId(skuId);
            skuSaleAttrValue.setSpuId(spuId);
            skuSaleAttrValue.setSaleAttrValueId(skuSaleAttrValueListDTO.getSaleAttrValueId());
            return skuSaleAttrValue;
        }).collect(Collectors.toList());
        skuSaleAttrValueService.saveBatch(skuSaleAttrValueList);

        // 4. 上传skuImageList
        List<SkuImage> skuImageList = skuImageListDTO.stream().map(skuImageDTO -> {
            SkuImage skuImage = new SkuImage();
            skuImage.setSpuImgId(skuImageDTO.getSpuImgId());
            skuImage.setImgName(skuImageDTO.getImgName());
            skuImage.setImgUrl(skuImageDTO.getImgUrl());
            skuImage.setSkuId(skuId);
            skuImage.setIsDefault("0");
            return skuImage;
        }).collect(Collectors.toList());
        skuImageService.saveBatch(skuImageList);
        redisTemplate.opsForValue().setBit("skuids:bitmap:",skuId,true);
    }

    @Override
    public SkuInfo getSkuInfo(Long skuId) {
        SkuInfo skuInfo = baseMapper.selectById(skuId);
        return skuInfo;
    }

    @Override
    public BigDecimal getPrice(Long skuId) {
        BigDecimal price = baseMapper.getPrice(skuId);
        return price;
    }

    @Override
    public void upGoods(Long skuId) {
        //修改数据库的上架状态
        boolean update = lambdaUpdate().eq(SkuInfo::getId, skuId).set(SkuInfo::getIsSale, 1).update();

        if (update){
            SkuInfo info = getById(skuId);
            Goods goods = new Goods();
            goods.setId(info.getId());
            goods.setDefaultImg(info.getSkuDefaultImg());
            goods.setTitle(info.getSkuName());
            goods.setPrice(info.getPrice().doubleValue());
            goods.setCreateTime(new Date());

            //查询品牌
            BaseTrademark trademark = baseTrademarkService.getById(info.getTmId());
            goods.setTmId(info.getTmId());
            goods.setTmName(trademark.getTmName());
            goods.setTmLogoUrl(trademark.getLogoUrl());


            //三级分类信息
            CategoryTreeVo tree = baseCategory1Service.getCateGoryTreeWithC3id(info.getCategory3Id());
            goods.setCategory1Id(tree.getCategoryId());
            goods.setCategory1Name(tree.getCategoryName());

            CategoryTreeVo child = tree.getCategoryChild().get(0);
            goods.setCategory2Id(child.getCategoryId());
            goods.setCategory2Name(child.getCategoryName());

            CategoryTreeVo child2 = child.getCategoryChild().get(0);
            goods.setCategory3Id(child2.getCategoryId());
            goods.setCategory3Name(child2.getCategoryName());

            //热度分
            goods.setHotScore(0L);

            //所有的平台属性
            List<SearchAttr> attrs = skuAttrValueService.getSkuAttrsAndValue(skuId);
            goods.setAttrs(attrs);

            //执行上架
            searchFeignClient.up(goods);
        }
    }

    @Override
    public void downGoods(Long skuId) {
        //1、修改数据库的上架状态
        boolean update = lambdaUpdate()
                .set(SkuInfo::getIsSale, 0)
                .eq(SkuInfo::getId, skuId).update();

        //2、远程下架
        if(update){
            searchFeignClient.down(skuId);
            //上下架的细节：缓存、bitmap都要同步
        }
    }
}




